package com.huixiaodian.baselibrary.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号码验证
 */

public class PhoneNumberUtil {

    /**
     * 手机号码验证
     * @param phoneNumber 验证手机号码
     * @return 手机号码是否正确
     */
    public static boolean isRight(String phoneNumber){
        return isLengthRight(phoneNumber) && isFormatRight(phoneNumber);
    }

    /**
     * 手机号码长度验证
     * @param phoneNumber 需要验证的手机号码
     * @return 手机号码长度是否为11位的数字
     */
    public static boolean isLengthRight(String phoneNumber){
        return phoneNumber.length()==11;
    }

    /**
     * 手机号码格式验证
     * @param phoneNumber 需要验证的手机号码
     * @return 手机号码是否属于已经发行的手机号段
     */
    private static boolean isFormatRight(String phoneNumber){
        Pattern mPattern = Pattern.compile("^((1[3,8][0-9])|(15[0,1,2,3,5,6,7,8,9])|(14[5,7,9])|(17[0,1,2,3,4,5,6,7,8])|(19[0,1,2,3,5,6,7,8,9])|(16[6]))\\d{8}$");
        Matcher matcher = mPattern.matcher(phoneNumber);
        return matcher.matches();
    }

    public static String phoneNumberFormat(String phoneNumber){
        if(isLengthRight(phoneNumber)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder = stringBuilder.append(phoneNumber.substring(0, 3))
                    .append("****").append(phoneNumber.substring(7));
            return stringBuilder.toString();
        }else {
            return phoneNumber;
        }

    }
}
